package com.riki.riki_push;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import com.riki.riki_push.util.CommonUtil;
import com.xiaomi.mipush.sdk.MiPushClient;

import java.lang.reflect.Method;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;


/**
 * RikiPushPlugin
 */
public class RikiPushOppoPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Context context;
    private Activity activity;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "riki_push_oppo");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {


        try {
            if (call.method.equals("initOppo")) {
                initOppo(call, result);
            } else if (call.method.equals("getOppoRegId")) {
                getOppoRegId(call, result);
            } else if (call.method.equals("unInit")) {
                unInit(call, result);
            } else {
                result.notImplemented();
            }
        } catch (Exception e) {
            result.notImplemented();
        }


       /* try {
            Method method = this.getClass().getDeclaredMethod(call.method, MethodCall.class, Result.class);
            method.invoke(this, call, result);
        } catch (Exception e) {
            result.notImplemented();
        }*/

    }


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }


    /**
     * 获取客户端的 RegId
     */
    private void getOppoRegId(@NonNull MethodCall call, @NonNull Result result) {
        try {
            if (!com.heytap.mcssdk.PushManager.isSupportPush(context)) {
                CommonUtil.runMainThreadReturn(result, 0);
                return;
            }
            String registerId = com.heytap.mcssdk.PushManager.getInstance().getRegisterID();
            CommonUtil.runMainThreadReturn(result, registerId);
        } catch (Exception e) {
            CommonUtil.runMainThreadReturn(result, 0);
        }
    }


    /**
     * 注册 oppo
     */
    private void initOppo(@NonNull MethodCall call, @NonNull Result result) {
        try {
            if (!com.heytap.mcssdk.PushManager.isSupportPush(context)) {
                result.success(0);
                return;
            }
            String appKey = CommonUtil.getParam(call, result, "appKey");
            String appSecret = CommonUtil.getParam(call, result, "appSecret");
            com.heytap.mcssdk.PushManager.getInstance().register(this.context, appKey, appSecret, null);
            CommonUtil.runMainThreadReturn(result, 1);
        } catch (Exception e) {
            CommonUtil.runMainThreadReturn(result, 0);
        }
    }


    /**
     * 申请oppo 推送的权限
     */
    private void applyOppoPermission(@NonNull MethodCall call, @NonNull Result result) {
        if (!com.heytap.mcssdk.PushManager.isSupportPush(context)) {
            result.success(1);
            return;
        }
        com.heytap.mcssdk.PushManager.getInstance().requestNotificationPermission();
        result.success(1);
    }


    /**
     * 反初始化 oppo
     */
    private void unInit(@NonNull MethodCall call, @NonNull Result result) {
        try {
            if (!com.heytap.mcssdk.PushManager.isSupportPush(context)) {
                CommonUtil.runMainThreadReturn(result, 0);
                return;
            }
            com.heytap.mcssdk.PushManager.getInstance().unRegister();
            CommonUtil.runMainThreadReturn(result, 1);
        } catch (Exception e) {
            CommonUtil.runMainThreadReturn(result, 0);
        }
    }


    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBinding) {
        this.activity = activityPluginBinding.getActivity();


    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

    }

    @Override
    public void onDetachedFromActivity() {

    }
}
